package com.luojialong.onlineJudge.otherPython;

import com.luojialong.onlineJudge.common.ErrorCode;
import com.luojialong.onlineJudge.exception.BusinessException;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class AIHelper {

    private static final String fatherDir = System.getProperty("user.dir");

    public static String analyzeCode(String code) throws IOException, InterruptedException {
        if (code == null || code == "") {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"提交代码为空");
        }
        String result = "";
        String path = fatherDir + File.separator + "otherPython" + File.separator + "codePreJudge.py";
        String[] args1 = new String[]{"python", path, code};//第二个为python脚本所在位置，后面的为所传参数（得是字符串类型）
        Process proc = Runtime.getRuntime().exec(args1);// 执行py文件
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "gb2312"));//解决中文乱码，参数可传中文
        String line = null;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        in.close();
        proc.waitFor();
        return result;
    }
}